/*
 * 类名称:ApplyOpenshopServiceImpl.java
 * 包名称:com.platform.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-09-05 00:19:55        lipengjun     初版做成
 *
 * Copyright (c) 2019-2019 微同科技
 */
package com.platform.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.dao.ApplyOpenshopDao;
import com.platform.dao.ShopsDao;
import com.platform.entity.ApplyOpenshopEntity;
import com.platform.entity.ShopsEntity;
import com.platform.entity.SysUserEntity;
import com.platform.entity.UserEntity;
import com.platform.service.ApplyOpenshopService;
import com.platform.service.ShopsService;
import com.platform.service.UserService;
import com.platform.utils.Query;
import com.platform.utils.QueryPlus;
import com.platform.utils.RRException;
import com.platform.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 申请开店信息记录表Service实现类
 *
 * @author lipengjun
 * @date 2019-09-05 00:19:55
 */
@Service("applyOpenshopService")
public class ApplyOpenshopServiceImpl implements ApplyOpenshopService {

    @Autowired
    private ApplyOpenshopDao applyOpenshopDao;
    @Autowired
    private ShopsService shopsService;
    @Autowired
    private ShopsDao shopsDao;
    @Autowired
    private UserService userService;
    @Override
    public List<ApplyOpenshopEntity> queryAll(Map<String, Object> params) {
        return applyOpenshopDao.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<ApplyOpenshopEntity> page = new QueryPlus<ApplyOpenshopEntity>(params).getPage();
        return page.setRecords(applyOpenshopDao.selectApplyOpenshopPage(page, params));
    }

    @Override
    public boolean add(ApplyOpenshopEntity applyOpenshop) {
        return applyOpenshopDao.insert(applyOpenshop)>0;
    }

    @Override
    @Transactional
    public boolean update(ApplyOpenshopEntity applyOpenshop) throws Exception {
        SysUserEntity userEntity1 = ShiroUtils.getUserEntity();
        applyOpenshop.setUpdateTime(new Date());
        //修改用户角色为店主
        if(applyOpenshop.getStatus().equals(1)&&applyOpenshop.getUserId()!=null){
            UserEntity userEntity = userService.queryObject(applyOpenshop.getUserId());
            userEntity.setUserRole("2");
            if(applyOpenshop.getIdCardPositiveUrl()!=null&&applyOpenshop.getIdCardAnywayUrl()!=null&&applyOpenshop.getStudentCardUrl()!=null) {
                userEntity.setIdCardPositiveUrl(applyOpenshop.getIdCardPositiveUrl());
                userEntity.setIdCardAnywayUrl(applyOpenshop.getIdCardAnywayUrl());
                userEntity.setStudentCardUrl(applyOpenshop.getStudentCardUrl());
                userEntity.setMobile(applyOpenshop.getMobile());
            }else{
                throw new RRException("附件不能为空！");
            }
            int update = userService.update(userEntity);
            if(update>0){
                //todo 创建店铺 需要添加判断该用户是否已有过店铺
                HashMap<String, Object> queryMap = new HashMap<>();
                queryMap.put("user_id", applyOpenshop.getUserId());
                ShopsEntity shopsEntity = new ShopsEntity();
                List<ShopsEntity> shopsEntities = shopsDao.selectByMap(queryMap);
                if(shopsEntities!=null && shopsEntities.size()>0){
                    ShopsEntity shopsEntity1 = shopsEntities.get(0);
                    shopsEntity1.setUpdateUserId(userEntity1.getUserId());
                    shopsEntity1.setUpdateTime(new Date());
                    shopsEntity.setBuildingId(applyOpenshop.getBuildingId());
                    shopsEntity.setSchoolId(applyOpenshop.getSchoolId());
                    shopsEntity.setBuildingName(applyOpenshop.getBuildingName());
                    shopsEntity.setSchoolName(applyOpenshop.getSchoolName());
                    shopsService.update(shopsEntity);
                }else {
                    shopsEntity.setCreateUserId(userEntity1.getUserId());
                    shopsEntity.setCreateTime(new Date());
                    shopsEntity.setBuildingId(applyOpenshop.getBuildingId());
                    shopsEntity.setSchoolId(applyOpenshop.getSchoolId());
                    shopsEntity.setBuildingName(applyOpenshop.getBuildingName());
                    shopsEntity.setSchoolName(applyOpenshop.getSchoolName());
                    shopsEntity.setShopName(userEntity.getNickname() + "的店铺");
                    shopsEntity.setShopPicUrl("https://gxsd-1257944118.cos.ap-shanghai.myqcloud.com/20191016/233215123bcbdd.jpg");//默认店铺图片
                    shopsEntity.setShopType(1);
                    shopsEntity.setUserId(userEntity.getId());
                    shopsEntity.setUserName(userEntity.getNickname());
                    shopsService.save(shopsEntity);
                }
                return applyOpenshopDao.updateById(applyOpenshop)>0;
            }
        }
        throw new RRException("操作失败");

    }

    @Override
    public boolean delete(Integer id) {
        return applyOpenshopDao.deleteById(id)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Integer[] ids) {
        return applyOpenshopDao.deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return  applyOpenshopDao.queryTotal(params);
    }

    @Override
    public List<ApplyOpenshopEntity> queryList(Query query) {
        return applyOpenshopDao.queryAll(query);
    }

    @Override
    public ApplyOpenshopEntity getById(Integer id) {
        return applyOpenshopDao.selectById(id);
    }
}
